package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理相关接口")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<RoleVo>> selectByPage(@RequestBody RoleDto roleDto, @PathVariable Integer pageNum, @PathVariable Integer pageSize){

        PageResponse<RoleVo> page = roleService.selectByPage(roleDto,pageNum,pageSize);
        return ResponseResult.success(page);

    }

    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation("根据角色查询选中的资源数据")
    public ResponseResult<List<String>> findCheckedResource(@PathVariable Long roleId){
        List<String> roles = roleService.findCheckedResource(roleId);
        return ResponseResult.success(roles);
    }

    @PatchMapping
    @ApiOperation("角色修改")
    public ResponseResult update(@RequestBody RoleDto roleDto){
        roleService.update(roleDto);

        return ResponseResult.success();
    }


    @DeleteMapping("/{roleId}")
    @ApiOperation("删除角色")
    public ResponseResult delete(@PathVariable Long roleId){
        roleService.delete(roleId);
        return ResponseResult.success();
    }

    @PutMapping
    @ApiOperation("添加角色")
    public ResponseResult addRole(@RequestBody RoleDto roleDto){
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }

    @PostMapping("/init-roles")
    @ApiOperation("角色下拉框")
    public ResponseResult<List<RoleVo>> initRoles(){
        List<RoleVo> roleVos = roleService.initRoles();
        return ResponseResult.success(roleVos);
    }
}
